﻿using Facebook;
using SPCoder.Core.Utils;
using SPCoder.HelperWindows;
using SPCoder.Utils;
using SPCoder.Utils.Nodes;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using f = Facebook;

namespace SPCoder.Facebook.Utils
{
    public class FBConnector : BaseConnector
    {

        public FBConnector(string connectorType) : this()
        { }

        SPCoderWebBrowser webBrowser;

        public FBConnector()
        {
            OneParamDelegate method = delegate(object url)
            {
                webBrowser = new SPCoderWebBrowser();
                webBrowser.BrowserNavigated = WebBrowser_Navigated;
                webBrowser.Navigate((Uri)url);
                webBrowser.ShowDialog();
                return null;
            };
            OpenBrowser = method;            
        }

        string accesstoken = null;

        public OneParamDelegate OpenBrowser { get; set; }

        //public static FacebookSessionClient FacebookSessionClient = new FacebookSessionClient(Constants.FacebookAppId);
        public override SPCoder.Utils.Nodes.BaseNode GetSPStructure(string siteUrl)
        {
            
            //f.FacebookClient  
            Uri url = GenerateLoginUrl(SPCoderConstants.FACEBOOK_APP_ID, null);
            OpenBrowser(url);
            BaseNode root = new BaseNode();
            var fb = new FacebookClient(accesstoken);

            dynamic result = fb.Get("me");
            var name = result.name;
            root.Title = "Facebook";
            root.SPObject = fb;

            BaseNode my = new BaseNode();
            my.RootNode = root;
            my.ParentNode = root;
            my.Title = name;
            my.SPObject = result;
            root.Children.Add(my);
            
            return root;
        }


        private Uri GenerateLoginUrl(string appId, string extendedPermissions)
        {                        
            dynamic parameters = new ExpandoObject();            
            parameters.client_id = appId;
            parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";

            // The requested response: an access token (token), an authorization code (code), or both (code token).
            parameters.response_type = "token";

            // list of additional display modes can be found at http://developers.facebook.com/docs/reference/dialogs/#display
            parameters.display = "popup";

            // add the 'scope' parameter only if we have extendedPermissions.
            if (!string.IsNullOrWhiteSpace(extendedPermissions))
                parameters.scope = SPCoderConstants.FACEBOOK_DEFAULT_SCOPE + "," + extendedPermissions;
            else
                parameters.scope = SPCoderConstants.FACEBOOK_DEFAULT_SCOPE;

            // generate the login url
            var fb = new FacebookClient();
            return fb.GetLoginUrl(parameters);
        }

        public object WebBrowser_Navigated(object navigatedUrl)
        {
            // whenever the browser navigates to a new url, try parsing the url.
            // the url may be the result of OAuth 2.0 authentication.

            Uri url = (Uri)navigatedUrl;
            var fb = new FacebookClient();
            FacebookOAuthResult result;
            if (fb.TryParseOAuthCallbackUrl(url, out result))
            {
                // The url is the result of OAuth 2.0 authentication
                if (result.IsSuccess)
                {
                    accesstoken = result.AccessToken;
                    webBrowser.Close();
                }
                else
                {
                    var errorDescription = result.ErrorDescription;
                    var errorReason = result.ErrorReason;
                }
            }
            else
            {
                // The url is NOT the result of OAuth 2.0 authentication.
            }
            return null;
        }

        public override string ImagesPath
        {
            get
            {
                return null;
            }
            set
            { }
        }

        public override bool IsImagesPathLocal
        {
            get
            {
                return false;
            }
            set
            { }
        }
    }
}
